package com.xingfudazi.web.controller.app;

import java.net.CacheRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.xingfudazi.acl.cache.CacheClient;
import com.xingfudazi.app.domain.AppEntrust;
import com.xingfudazi.app.domain.AppMoment;
import com.xingfudazi.app.dto.MemberAvatar;
import com.xingfudazi.app.service.IAppEntrustService;
import com.xingfudazi.common.annotation.Log;
import com.xingfudazi.common.core.controller.BaseController;
import com.xingfudazi.common.core.domain.AjaxResult;
import com.xingfudazi.common.core.domain.Result;
import com.xingfudazi.common.core.page.TableDataInfo;
import com.xingfudazi.common.enums.BusinessType;
import com.xingfudazi.common.utils.poi.ExcelUtil;
import com.xingfudazi.web.vo.AppMomentAvatarVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 委托关系Controller
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/app/entrust")
public class AppEntrustController extends BaseController {
    @Autowired
    private IAppEntrustService appEntrustService;

    @Autowired
    private CacheClient cacheClient;

    /**
     * 查询委托关系列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppEntrust appEntrust) {
        startPage();
        List<AppEntrust> list = appEntrustService.selectAppEntrustList(appEntrust);
        return getDataTable(list);
    }

    /**
     * 导出委托关系列表
     */
    @Log(title = "委托关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppEntrust appEntrust) {
        List<AppEntrust> list = appEntrustService.selectAppEntrustList(appEntrust);
        ExcelUtil<AppEntrust> util = new ExcelUtil<AppEntrust>(AppEntrust.class);
        util.exportExcel(response, list, "委托关系数据");
    }

    /**
     * 获取委托关系详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appEntrustService.selectAppEntrustById(id));
    }

    /**
     * 新增委托关系
     */
    @Log(title = "委托关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(String code) {
        return toAjax(appEntrustService.insertAppEntrust(code));
    }

    /**
     * 修改委托关系
     */
    @Log(title = "委托关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppEntrust appEntrust) {
        return toAjax(appEntrustService.updateAppEntrust(appEntrust));
    }




    @GetMapping("code")
    public Result<String> code() {
        String code = this.appEntrustService.code();
        if (code ==null) {
            return Result.fail("当前添加繁忙");
        }
        return Result.success(code);
    }


    /**
     * 查询动态列表
     */
    @GetMapping("/turn")
    public Result<List<MemberAvatar>> turn(@RequestParam(defaultValue = "20") Integer pageSize,
                                           Long lastId, String search) {
        List<MemberAvatar> memberAvatarList = appEntrustService.turn(pageSize, lastId, search);
        return Result.success(memberAvatarList);
    }


}
